package com.atguigu.gmall.all.controller;

import com.atguigu.gmall.cart.client.CartFeignClient;
import com.atguigu.gmall.common.result.Result;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;

@Controller
public class CartController {

    @Resource
    private CartFeignClient cartFeignClient;

    @GetMapping("/addCart.html")
    public String addCart(Long skuId, Integer skuNum, RedirectAttributes redirectAttributes){
        cartFeignClient.addToCart(skuId, skuNum);
        redirectAttributes.addAttribute("skuId",skuId);
        redirectAttributes.addAttribute("skuNum",skuNum);
        return "redirect:http://cart.gmall.com/addToCart.html";
    }

    @GetMapping("/addToCart.html")
    public String addToCart(Long skuId, Integer skuNum, Model model){
        Result result = cartFeignClient.getCartInfo(skuId);
        if (result.getData() != null){
            model.addAttribute("cartInfo",result.getData());
            return "/cart/addCart";
        } else {
            //之前未登录时有购物车 用户突然登录了 没有购物车 导致空指针异常  走首页
            return "redirect:http://www.gmall.com";
        }
    }

    @GetMapping("/cart.html")
    public String cart(){
        return "cart/index";
    }

}
